package com.hera.base.web.vaadin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.hera.base.util.Constantes;
import com.hera.base.util.FiltroDTO;
import com.hera.base.util.TableCustom;
import com.hera.base.web.BaseUI;
import com.hera.process.config.rest.delegate.WorkflowDelegate;
import com.hera.process.security.domain.User;
import com.hera.process.workflow.domain.ProcessInstance;
import com.hera.process.workflow.domain.TaskUser;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.server.Resource;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class Bandeja extends BaseUI implements ClickListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4056949495101317588L;


	@AutoGenerated
	private VerticalLayout mainLayout;
	
	@AutoGenerated
	private GridLayout gridLayout_2;


	@AutoGenerated
	private TableCustom tblBandeja;


	@AutoGenerated
	private Button btnAdd;


	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
	private static Logger log = Logger.getLogger(Bandeja.class);
	
	private int tipoBandeja;
	
//	@Autowired
//	SeguridadService seguridadService;
	
	@Autowired
	private WorkflowDelegate workflowDelegate;
	
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public Bandeja() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		cargarFormulario();
	}
	
	private void cargarFormulario() {
		Resource resCabecera=new ThemeResource("images/workflow.png");
		//imgCabezera.setSource(resCabecera);
		//btnLogin.addClickListener(this);
		
		
		tblBandeja.addColumn("idTask", "Identificador", 90, true, String.class);
		tblBandeja.addColumn("name", "Tarea", 200, true, String.class);
		tblBandeja.addColumn("processInstance.name", "Proceso", 200, true, String.class);
		tblBandeja.addColumn("user.fullName", "Empleado", 300, true, String.class);
		tblBandeja.construir();
		/*
		tblBandeja.addColumn("", "Indicador", 150, true, String.class);*/
	}
	
	public void mostrarDatos(Integer bandeja) {
		tipoBandeja = bandeja;
		FiltroDTO filtroDTO = new FiltroDTO();
		User u= (User)getSession().getAttribute(Constantes.USUARIO.SESSION_NAME);
		List<TaskUser> listado=null;
		if(Constantes.BANDEJA.ENTRADA == bandeja) {
			filtroDTO.addParametro("user.idUser", u.getIdUser());
			listado = workflowDelegate.findAllTaskUsers(filtroDTO);
		} else if(Constantes.BANDEJA.USUARIO == bandeja) {
			listado=workflowDelegate.findBandejaUsuario(u.getIdUser());
		}else if(Constantes.BANDEJA.SOLICITADO==bandeja){
			filtroDTO.addParametro("user.idUser", u.getIdUser());
			filtroDTO.addParametro("idStatus", Constantes.ESTADOTAREA.PENDIENTE);
			listado = workflowDelegate.findAllTaskUsers(filtroDTO);
		}else if(Constantes.BANDEJA.BORRADOR==bandeja){
			filtroDTO.addParametro("user.idUser", u.getIdUser());
			filtroDTO.addParametro("idStatus", Constantes.ESTADOTAREA.BORRADOR);
			listado = workflowDelegate.findAllTaskUsers(filtroDTO);		
		}
		else if(Constantes.BANDEJA.ELIMINADO==bandeja){
			filtroDTO.addParametro("user.idUser", u.getIdUser());
			filtroDTO.addParametro("idStatus", Constantes.ESTADOTAREA.CANCELADO);
			listado = workflowDelegate.findAllTaskUsers(filtroDTO);		
		}
		
		tblBandeja.setLstData(listado);
	}
	
	public void recargarFormulario() {
		List<TaskUser> listado = new ArrayList<TaskUser>();
		
		for (int i = 0; i < 10; i++) {
			TaskUser task = new TaskUser();
			task.setIdTask(new Long(i));
			task.setName("Tarea " + tipoBandeja + "_" + i);
			ProcessInstance process = new ProcessInstance();
			process.setName("Proceso " + tipoBandeja);
			task.setProcessInstance(process);
			task.setRequestedDate(new Date());
			listado.add(task);
		}
		
		tblBandeja.setLstData(listado);
	}	
	
	@Override
	protected void postConstruct() {
		btnAdd.addClickListener(this);
		
	}

	public void buttonClick(ClickEvent event) {
		if(event.getButton().equals(btnAdd)) {
			irProceso();
		}
		
	}
	private void irProceso(){
		Window window=new Window();
		window.setStyleName("titulo-bold");
		window.setModal(true);
		window.setResizable(false);
		window.setCaption("Lista de proceso");
		ProcessUI processUI =new ProcessUI();
		VerticalLayout layout=new VerticalLayout();
		layout.addComponent(processUI);
		layout.setMargin(true);
		window.setContent(processUI);	
		window.setHeight("500");
		window.setWidth("800");
		UI.getCurrent().addWindow(window);
		System.out.println("btn +");
		processUI.llenarProcesos();
	}
	
	
	 

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// gridLayout_2
		gridLayout_2 = buildGridLayout_2();
		mainLayout.addComponent(gridLayout_2);
		mainLayout.setComponentAlignment(gridLayout_2, new Alignment(20));
		
		return mainLayout;
	}

	@AutoGenerated
	private GridLayout buildGridLayout_2() {
		// common part: create layout
		gridLayout_2 = new GridLayout();
		gridLayout_2.setImmediate(false);
		gridLayout_2.setWidth("100.0%");
		gridLayout_2.setHeight("100.0%");
		gridLayout_2.setMargin(false);
		gridLayout_2.setRows(2);
		
		// btnAdd
		btnAdd = new Button();
		btnAdd.setCaption("+");
		btnAdd.setImmediate(true);
		btnAdd.setWidth("-1px");
		btnAdd.setHeight("-1px");
		gridLayout_2.addComponent(btnAdd, 0, 0);
		gridLayout_2.setComponentAlignment(btnAdd, new Alignment(48));
		
		// tblBandeja
		tblBandeja = new TableCustom();
		tblBandeja.setImmediate(false);
		tblBandeja.setWidth("100.0%");
		tblBandeja.setHeight("100.0%");
		gridLayout_2.addComponent(tblBandeja, 0, 1);
		gridLayout_2.setComponentAlignment(tblBandeja, new Alignment(20));
		
		return gridLayout_2;
	}
}
